
    /**  
    * @Title: QqUtil.java
    * @Package com.third.qq
    * @Description: 
    * @author likai
    * @date 2017年12月1日
    * @version V1.0  
    */
    
package com.third.utils.qq;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.supers.enums.Charset;
import com.utils.http.HttpClientUtil;
import com.utils.transtype.JsonObjectUtil;

/**
    * @ClassName: QqUtil
    * @Description: qq开发工具，接口网址:http://wiki.open.qq.com/wiki/%E9%A6%96%E9%A1%B5
    * @author likai
    * @date 2017年12月1日
    *
    */

public class QqUtil{
	public static Logger log = LoggerFactory.getLogger(QqUtil.class);
	private String appId;//qq开发账号
	private String appSecret;//qq开发密钥
	private static QqUtil qqUtil;

	private QqUtil(QqConfig qqConfig) {
		super();
		appId=qqConfig.getAppId();
		appSecret=qqConfig.getAppSecret();
	}
	
	
	public static QqUtil create(QqConfig qqConfig) {
		if (qqUtil==null) {
			qqUtil=new QqUtil(qqConfig);
		}
		return qqUtil;
	}
	
	/* 
	    * @Description:获取登录链接
	    * 
	    * @return:
	    * 
	    * 
	    *
	    */
	public  String getLoginUrl(String redirectUri) {
		return "https://graph.qq.com/oauth2.0/authorize?client_id="+appId+"&redirect_uri="+redirectUri+"&response_type=token&display=mobile&state=";
		
	}
	
	/* 
	    * @Description:通过code换取网页授权access_token、
	    * 
	    * @return:
	    * 
	    * access_token	用户授权的唯一票据
		* expires_in	access_token接口调用凭证超时时间，单位（秒）
	    *
	    */
	@SuppressWarnings("unchecked")
	public  Map<String, Object> getAccessTokenByCode(String code,String redirectUri) {
		try {
			String url="https://graph.qq.com/oauth2.0/token?client_id="+appId+"&client_secret="+appSecret+"&grant_type=authorization_code"+"&code="+code+"&redirect_uri="+redirectUri;
			Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, Map.class);
			if (response.get("error")==null) {				
				return response;
			}else {				
				log.info("QQ getAccessTokenByCode出错："+response.toString());
				return null;
			}
		} catch (Exception e) {
			log.info("换取QQ网页授权access_token失败");
			return null;
		}
	}
	
	/* 
	    * @Description:access_token获取用户OpenId
	    * 
	    * @return:
	    * 
	    * 
	    * openid	用户ID
	    *
	    */
	@SuppressWarnings("unchecked")
	public  String getUserOpenId(String accessToken) throws Exception {
		String url="https://graph.qq.com/oauth2.0/me?access_token="+accessToken;
		String response=(String) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, String.class);
		if (response==null) {
			log.error("调用getUserOpenId请求失败");
			return null;
		}
		response=response.substring(response.indexOf("(")+1, response.indexOf(")"));
		Map<String, Object> result=JsonObjectUtil.ALWAYS.jsonToObject(response, Map.class);
		if (result.get("error")==null) {
			return (String) result.get("openid");
		}else {
			log.error("调用getUserOpenId接口出错，返回："+response);
			return null;
		}
	}
	
	/* 
	    * @Description:access_token获取用户信息
	    * 
	    * @return:
	    * 
	    * 
	    * openid	用户ID
	    *
	    */
	@SuppressWarnings("unchecked")
	public  Map<String, Object> getUserInfo(String accessToken) {
		try {
			String openId=getUserOpenId(accessToken);
			if (openId==null) {
				return null;
			}
			String url="https://graph.qq.com/user/get_user_info?access_token="+accessToken+"&oauth_consumer_key="+appId+"&openid="+openId;
			Map<String, Object> response=(Map<String, Object>) HttpClientUtil.httpJson.getClient(url, Charset.UTF8, Map.class);
			if ((Integer)response.get("ret")==0) {		
				response.put("openId", openId);
				return response;
			}else {				
				log.info("QQ getUserInfo出错："+response.toString());
				return null;
			}
		} catch (Exception e) {
			log.info("获取QQ用户信息失败");
			return null;
		}
	}
	
	
}
